package com.huarui.purchaseOrder.service.provider.task;

import com.huarui.dictionary.DictionaryEnum;
import com.huarui.dictionary.service.DictionaryReadOnlyService.DictionaryResource;
import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.task.model.Task;
import com.huarui.task.model.TaskDetail;
import com.huarui.task.service.provider.TaskProvider;
import com.huarui.task.taskEnum.WorkFlowTaskNameEnum;

public class PurOrSaleUpdateTaskProviderImpl implements TaskProvider{

	private PurOrSaleOrder orders;
	
	private TaskDetail lastTaskDetail;
	
	private Task task;
	
	
	public PurOrSaleUpdateTaskProviderImpl(PurOrSaleOrder orders, Task task, TaskDetail lastTaskDetail) {
		super();
		this.orders = orders;
		this.task = task;
		this.lastTaskDetail = lastTaskDetail;
	}
	
	@Override
	public Task createTask() {
		return null;
	}

	@Override
	public Task updateTask() {
		return this.updateTask(this.task, this.lastTaskDetail, this.orders);
	}

	private Task updateTask(Task task, TaskDetail lastTaskDetail, PurOrSaleOrder orders) {
		
		task.setPreActionCompanyId(lastTaskDetail.getActionCompanyId());
		task.setPreActionCompanyName(lastTaskDetail.getActionCompanyName());
		
		task.setPreActorId(lastTaskDetail.getActorId());
		task.setPreActorName(lastTaskDetail.getActorName());
		
		Integer preStatus = task.getStatus().toString().equals(orders.getStatus()) ? task.getPreStatus() : task.getStatus();
		task.setPreStatus(preStatus);
		task.setPreStatusId(preStatus);
		task.setPreStatusName(DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_ORDER_STATUS, preStatus.toString()));
		
		task.setStatus(Integer.parseInt(orders.getStatus()));
		task.setStatusId(Integer.parseInt(orders.getStatus()));
		task.setStatusName(DictionaryResource.getDictionaryValue(DictionaryEnum.GROUP_ORDER_STATUS, orders.getStatus()));
		
		task.setCurrentorId(orders.getCurrentorId());
		task.setCurrentorName(orders.getCurrentorName());
		
		task.setCurrentCompanyId(orders.getCurrentCompanyId());
		task.setCurrentCompanyName(orders.getCurrentCompanyName());
		
		if( 
				(WorkFlowTaskNameEnum.WORKFLOWID_TASKNAME_NOT_ONLINE_SALE_ORDER.getWorkFlowId().equals(orders.getWorkFlowId()) && 
				PurOrSaleOrder.STATUS_SELLER_DELIVERY.equals(orders.getStatus()) )
					|| 
				PurOrSaleOrder.STATUS_BUYER_ACPT.equals(orders.getStatus()) ) {
			task.setFinished(Boolean.TRUE);
		}
		
		if(PurOrSaleOrder.STATUS_INVALIDE.equals(orders.getStatus())) {
			task.setRevoked(Boolean.TRUE);
		}
		
		if(PurOrSaleOrder.STATUS_DELETED.equals(orders.getStatus())) {
			task.setRevoked(Boolean.TRUE);
		}
		
		return task;
	}
	
}
